<script setup lang="ts">
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'

// 导入子组件CustomNavbar
import CustomNavbar from '../../components/CustomNavbar.vue'
// 导入分类面板组件
import CategoryPanel from './components/CategoryPanel.vue'
// 导入热门推荐组件
import HotPanel from './components/HotPanel.vue'
// 导入骨架屏组件
import PageSkeleton from './components/PageSkeleton.vue'

/* 获取轮拨图数据*/
import { getHomeBannerAPI } from '@/services/home'
import type { BannerItem } from '@/types/home'
const bannerList = ref<BannerItem[]>([])
const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI()
  bannerList.value = res.result
}

/*获取分类数据*/
import { getHomeCategoryAPI } from '@/services/home'
import type { CategoryItem } from '@/types/home'
const categoryList = ref<CategoryItem[]>([])
const getHomeCategoryData = async () => {
  const res = await getHomeCategoryAPI()
  categoryList.value = res.result
}

/*获取热门推荐数据*/
import { getHomeHotAPI } from '@/services/home'
import type { HotItem } from '@/types/home'
const hotList = ref<HotItem[]>([])
const getHomeHotData = async () => {
  const res = await getHomeHotAPI()
  hotList.value = res.result
}

/**滚动触底时加载分页 */
// import type { XtxGuessInstance } from '@/types/component'
// 获取XtxGuess组件实例
// const guessRef = ref<XtxGuessInstance>()
// 滚动触底事件
// const onScrolltolower = () => {
//   guessRef.value?.getMore() //调用子组件方法getMore()
// }

// 加载composable/index.ts
import { useGuessList } from '@/composables'
const { guessRef, onScrolltolower } = useGuessList()

/**下拉刷新 */
const isTriggered = ref(false)
// 监听用户下拉行为
const onRefresherrefresh = async () => {
  // 开启动画
  isTriggered.value = true
  // 重置"猜你喜欢"组件数据
  guessRef.value?.resetData()
  // 刷新数据(重新获取数据):确保全部加载完毕后关闭动画
  await Promise.all([
    getHomeBannerData(),
    getHomeCategoryData(),
    getHomeHotData(),
    guessRef.value?.getMore(),
  ])
  // 关闭动画
  isTriggered.value = false
}

// 判断骨架屏的加载时机
const isLoading = ref(false)
onLoad(() => {
  isLoading.value = true
  Promise.all([getHomeBannerData(), getHomeCategoryData(), getHomeHotData()]).then(() => {
    isLoading.value = false
  })
})
</script>

<template>
  <!-- 自定义导航栏 -->
  <CustomNavbar />
  <!-- 滚动容器绑定触底事件 -->
  <scroll-view
    refresher-enabled
    @refresherrefresh="onRefresherrefresh"
    :refresher-triggered="isTriggered"
    @scrolltolower="onScrolltolower"
    class="scroll-view"
    scroll-y
  >
    <PageSkeleton v-if="isLoading" />
    <template v-else>
      <!-- 轮拨图通用子组件 -->
      <!-- 父传子:自定义属性为list -->
      <XtxSwiper :list="bannerList" />
      <!-- 分类面板 -->
      <CategoryPanel :list="categoryList" />
      <!-- 热门推荐 -->
      <HotPanel :list="hotList" />
      <!-- 猜你喜欢:设置模板标识获取子组件暴露的属性和方法 -->
      <XtxGuess ref="guessRef" />
    </template>
  </scroll-view>
</template>

<style lang="scss">
page {
  //更改页面的底色
  background-color: #f5f5f5;
  //设置页面高度为100%
  height: 100%;
  // 设置弹性布局和排列方向
  display: flex;
  flex-direction: column;
}
.scroll-view {
  flex: 1;
  //height:0//若还滚动不了就加上这句
}
</style>
